﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class User
    {
        public static User FindByUserID_IncludeDeleted(linqDatabaseDataContext db, int UserID)
        {
            var subquery = (from u in db.Users where u.ID == (from v in db.Users where v.UserID == u.UserID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.UserID == UserID select u).SingleOrDefault());
        }

        public static User FindByUserID(linqDatabaseDataContext db, int UserID)
        {
            var subquery = (from u in db.Users where u.ID == (from v in db.Users where v.UserID == u.UserID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.UserID == UserID select u).SingleOrDefault());
        }

        public static User FindByUsername(linqDatabaseDataContext db, string Username)
        {
            var subquery = (from u in db.Users where u.ID == (from v in db.Users where v.UserID == u.UserID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.Username == Username select u).SingleOrDefault());
        }

        public static List<User> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Users where u.ID == (from v in db.Users where v.UserID == u.UserID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery select u).ToList());
        }

        public static List<User> ToList(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Users where u.ID == (from v in db.Users where v.UserID == u.UserID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted select u).ToList());
        }

        public static List<User> HistoryToListByUserID(linqDatabaseDataContext db, int UserID)
        {
            return (from u in db.Users where u.UserID == UserID orderby u.Timestamp ascending select u).ToList();
        }
    }
}
